home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15 / Database / TransactionAwareObject.h < prev   
Encoding:
Text File  |  1996-04-25  |  5.2 KB  |  159 lines  |  [TEXT/CWIE]

  1. //================================================================================
  2. // Greg Anderson
  3. // db+
  4. //
  5. // Abstract base class for objects that can have update pointers, participate
  6. // in transactions, and so on.
  7. // 16, 17 May 1994
  8. //================================================================================
  9. #pragma once
  10.  
  11. #ifndef __TRANSACTIONAWAREOBJECT__
  12. #define __TRANSACTIONAWAREOBJECT__
  13.  
  14. #include "Int64.h"
  15.  
  16. class TTransaction;
  17. // class TAbstractUpdatePointer;
  18.  
  19. class TDBRecord;
  20. class TDBElement;
  21. class TDBProperty;
  22. class TDataRecord;
  23.  
  24. const OSErr eAlreadyInTransaction = -31222;
  25. const OSErr eNotInTransaction = -31223;
  26.  
  27. #define kNoTransaction nil
  28.  
  29. //================================================================================
  30. // class TTransactionBaseObject
  31. //
  32. // Both update pointers and transaction-aware objects are derived from
  33. // the class TTransactionBaseObject.
  34. //================================================================================
  35. class TTransactionBaseObject
  36. {
  37.     //
  38.     // TAbstractUpdatePointer is a friend class because only the update pointer
  39.     // is allowed to call the commit / discard changes method of an object
  40.     //
  41.     friend class TAbstractUpdatePointer;
  42.  
  43. private:
  44.     long                        fReferenceCount;
  45.     
  46. public:
  47.             TTransactionBaseObject() : fReferenceCount(0) {};
  48.     virtual    ~TTransactionBaseObject() {};
  49.  
  50.     //
  51.     // The object key and object key space are used to uniquely
  52.     // define an object, so that a transaction may determine if
  53.     // it already has an update pointer for a given object.
  54.     // 
  55.     virtual Int64                ObjectsKeySpace() const = 0;
  56.     virtual long                ObjectKey() const = 0;
  57.     
  58.     //
  59.     // Convenience function for comparing keys:
  60.     //
  61.     Boolean                        KeysMatch(const TTransactionBaseObject& test) const
  62.                                 {
  63.                                     return (this->ObjectsKeySpace() == test.ObjectsKeySpace()) && (this->ObjectKey() == test.ObjectKey());
  64.                                 };
  65.  
  66.  
  67. //    void                        AddReference() const            { ++((TTransactionBaseObject*)this)->fReferenceCount; }
  68. //    void                        RemoveReference() const            { --((TTransactionBaseObject*)this)->fReferenceCount; };
  69.     virtual void                AddReference() const;
  70.     virtual Boolean                RemoveReference() const;
  71.     Boolean                        HasReference() const            { return fReferenceCount > 0; }
  72.  
  73.     //
  74.     // Delete this if we can
  75.     //
  76.     virtual void                DisposeRecordIfUnreferenced();
  77.     
  78. protected:
  79.     //
  80.     // A transaction will call either commit changes or discard changes,
  81.     // but never both.
  82.     //
  83.     virtual void                PrepareToCommit(TTransaction* transaction);
  84.     virtual void                CommitChanges(TTransaction* transaction) = 0;
  85.     virtual void                DiscardChanges(TTransaction* transaction) = 0;
  86. };
  87.  
  88. //================================================================================
  89. // class TTransactionAwareObject
  90. //
  91. // Any object that can create an update pointer and be part of a transaction
  92. // is derived from the class TTransactionAwareObject
  93. //================================================================================
  94. class TTransactionAwareObject : public TTransactionBaseObject
  95. {
  96.     //
  97.     // TTransaction is a friend class because only the transaction
  98.     // is allowed to ask for the update pointer for an object.
  99.     //
  100.     friend class TTransaction;
  101.  
  102. private:
  103.     TTransaction*                fTransaction;
  104.     
  105. public:
  106.             TTransactionAwareObject() : fTransaction(nil) {};
  107.     virtual ~TTransactionAwareObject() {};
  108.     
  109.     Boolean                        InTransaction() const { return fTransaction != nil; };
  110.     Boolean                        InTransaction(TTransaction* t) const { return fTransaction == t; };
  111.  
  112.     //
  113.     // This method should really be protected
  114.     //
  115.     TTransaction*                Transaction() const { return fTransaction; };
  116.     
  117.     //
  118.     // Objects that can belong to transactions must be able
  119.     // to create an update pointer and commit and discard changes;
  120.     // However, only their friends can ask them to do it (in this
  121.     // case, only a transaction object will call these methods).
  122.     //
  123.     // IMPORTANT:  Note that the following methods are all declared
  124.     // to be 'const', even though they change fields of the object.
  125.     // It is very important that these methods be declared const so 
  126.     // that cursors with const-only access can call them.  It isn't
  127.     // too terrible to call these methods 'const', though, since they
  128.     // are only changing cached references (to the object's transaction
  129.     // and update pointer), and are not actually changing the state
  130.     // of the data the object represents.
  131.     //
  132. protected:
  133.     void                            SetTransaction(TTransaction* t) const { ((TTransactionAwareObject*)this)->fTransaction = t; }
  134.  
  135.     //
  136.     // GrabUpdatePointer should create an update pointer, or fail if one already exists
  137.     // GetUpdatePointer should return the update pointer that already exists for the object, if any
  138.     // ReleaseUpdatePointer is called by the transaction after CommitChanges or DiscardChanges
  139.     // is called, but before the update pointer is actually deleted.
  140.     //
  141.     virtual TTransactionAwareObject*    GrabUpdatePointer(TTransaction* transaction) const;
  142.  
  143. #if 0
  144.     virtual const TDBRecord* AbstractDBRecord() const;
  145.     virtual const TDBElement* DBElementRecord() const;
  146.     virtual const TDBProperty* DBPropertyRecord() const;
  147.     virtual const TDataRecord* DataRecord() const;    
  148. #endif
  149.  
  150.     virtual  TDBRecord* AbstractDBRecord() const;
  151.     virtual  TDBElement* DBElementRecord() const;
  152.     virtual  TDBProperty* DBPropertyRecord() const;
  153.     virtual  TDataRecord* DataRecord() const;    
  154.  
  155. };
  156.  
  157.  
  158. #endif
  159.